<template>
<div>
  <!-- 面包屑导航 -->
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/AdminIndex' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>产品管理</el-breadcrumb-item>
    <el-breadcrumb-item>产品信息</el-breadcrumb-item>
  </el-breadcrumb>

  <!-- 搜索导航栏 -->
  <div class="con" >
    <div style="float: left">
      <el-button type="primary" icon="el-icon-edit"  @click="addDialogVisible = true">新建</el-button>
      <el-button icon="el-icon-refresh" @click="refreshgetUserList"></el-button>
    </div>

    <div style="display: inline-block;width: 80%;float: right">
      <!-- 搜索添加 -->
      <el-select v-model="q_product_kind_id" placeholder="产品类别" style="width: 15%;margin-right: 10px;margin-left: 200px" @change="queryChange">
        <el-option
            v-for="item in this.q_kindList"
            :key="item.product_kind_id"
            :label="item.kind"
            :value="item.product_kind_id">
        </el-option>
      </el-select>
      <el-input  placeholder="输入产品编号" clearable v-model="q_account" @input="queryChange"  @clear="queryChange" style="width: 15%;margin-right: 10px"></el-input>
      <el-input  placeholder="输入产品名称" clearable v-model="q_name" @input="queryChange"   @clear="queryChange" style="width: 15%;margin-right: 10px"></el-input>
      <el-input  type="number" placeholder="价格下限" clearable v-model="q_low"  @blur="testhigh" @input="queryChange"   @clear="queryChange" style="width: 15%;margin-right: 10px"></el-input>
      <el-input  placeholder="价格上限" clearable v-model="q_high" @input="queryChange"
                 @blur="testhigh"
                 @clear="queryChange" style="width:15%;margin-right: 10px"></el-input>
      <el-button icon="el-icon-search" @click="getUserList"></el-button>
    </div>
  </div>


  <!-- 信息展示栏 -->
  <div class="con">
    <el-table :data="userList" border stripe>
      <el-table-column label="序号"  type="index" width="50px"></el-table-column>
      <el-table-column label="产品编号" prop="p_account"></el-table-column>
      <el-table-column label="产品名称" prop="p_name"></el-table-column>
      <el-table-column label="产品类别" prop="kind"></el-table-column>
      <el-table-column label="产品规格" prop="p_specification"></el-table-column>
      <el-table-column label="产品价格" prop="p_price"></el-table-column>
      <el-table-column label="产品简介" prop="p_brief"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- 修改 -->
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row)"></el-button>
          <!-- 删除 -->
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteUser(scope.row.product_id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px">
      <!-- 分页 size-change 每页最大数变化 current-change 页数变化 layout 功能组件-->
      <el-pagination
          style="text-align: center; "
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 5, 9, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </div>




  </div>

  <!-- 创建新用户对话框 -->
  <el-dialog title="添加种类" :visible.sync="addDialogVisible" width="40%"
             @close="addDialogClosed" >

      <el-form :model="form1" :rules="rules1" ref="ruleForm1" label-width="80px" >
        <!-- 姓名 -->
        <el-form-item label="产品名称" prop="p_name" required>
          <el-input v-model="form1.p_name"></el-input>
        </el-form-item>

        <el-form-item label="产品类别" prop="product_kind_id" required>
          <el-select v-model="form1.product_kind_id" placeholder="请选择" style="width: 100%">
            <el-option
                v-for="item in this.kindList"
                :key="item.product_kind_id"
                :label="item.kind"
                :value="item.product_kind_id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品价格" prop="p_price" required>
          <el-input type="number" v-model="form1.p_price"></el-input>
        </el-form-item>

        <el-form-item label="产品规格" prop="p_account">
          <el-input v-model="form1.p_specification"></el-input>
        </el-form-item>
        <el-form-item label="产品简介" prop="p_account">
          <el-input v-model="form1.p_brief"></el-input>
        </el-form-item>
      </el-form>

    <!-- 内容底部区域 -->
    <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
  </el-dialog>

  <!-- 修改用户对话框 -->
  <el-dialog title="修改产品信息" :visible.sync="editDialogVisible" width="30%" @colse="editDialogClosed">
    <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">

      <!-- 姓名 -->
      <el-form-item label="编号" prop="p_account">
        <el-input v-model="editForm.p_account"  disabled></el-input>
      </el-form-item>

      <!-- 姓名 -->
      <el-form-item label="类别" prop="p_account">
        <el-input v-model="editForm.kind"  disabled></el-input>
      </el-form-item>

      <!-- 姓名 -->
      <el-form-item label="名称" prop="p_name" required>
        <el-input v-model="editForm.p_name" ></el-input>
      </el-form-item>

      <!-- 姓名 -->
      <el-form-item label="价格" prop="p_price" required>
        <el-input v-model="editForm.p_price" ></el-input>
      </el-form-item>
      <!-- 姓名 -->
      <el-form-item label="规格" prop="p_specification">
        <el-input v-model="editForm.p_specification" ></el-input>
      </el-form-item>

      <!-- 姓名 -->
      <el-form-item label="简介" prop="p_brief">
        <el-input v-model="editForm.p_brief" ></el-input>
      </el-form-item>



    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo">确 定</el-button>
      </span>
  </el-dialog>

</div>
</template>

<script>
export default {
  name: "Good",
  data(){
    return{
      q_product_kind_id :"",
      q_account:'',
      q_name:'',
      q_kindList:[],
      q_low:'',
      q_high:'',


      // 请求数据
      queryInfo: {
        query: "",
            pageNum: 1,
            pageSize: 9
      },
      //新建用户
      form1:{
        product_kind_id:'',
        p_specification:'',
        p_name:'',
        p_price:'',
        p_brief:'',
      },
      //修改窗口
      editForm:{
        kind:'',
        p_brief:'',
        product_id:'',
        p_specification:'',
        p_name:'',
        p_account:'',
        p_price: '',
      },
      //修改窗口
      editDialogVisible:false,
      editFormRules:{
        p_name:[
          { required: true, message: "请输入名称", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],
      },
      //判断框
      rules1:{
        kind:[
          { required: true, message: "请输入种类", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],
      },
      addDialogVisible:false,
      ban:false,
      choice:true,
      userList:[],
      kindList:[],
          total:0,
      querySum:'',
    }

  },
  created() {
    this.getUserList();
  },
  methods:{
    testhigh(){
      if(parseInt(this.q_high)<parseInt(this.q_low) ){
        this.$message.info("价格上限应大于价格下限");
        this.q_high=''
      }
    },

      queryChange(){
        this.queryInfo.query = this.q_product_kind_id+" ~"+this.q_account+" ~"+this.q_name+" ~"+this.q_low+" ~"+this.q_high+" ~";
        console.log(this.queryInfo.query )
      },
    async refreshgetUserList(){
        this.q_product_kind_id =""
        this.q_account=''
        this.q_name=''
        this.q_low=''
        this.q_high=''
        this.queryChange()

        this.getUserList()
    },

      async getUserList(){
        const {data:res} = await this.$http.get("allProduct",{params:this.queryInfo});
        const {data:res1} = await this.$http.get("ProductKindIndex");

        this.userList=res.data;
        this.kindList=res1.data;
        this.q_kindList=[]
        this.kindList.forEach(value => {
          this.q_kindList.push(value)
        })
        this.q_kindList.push({kind:"ALL",product_kind_id:"-1",})

        this.total=res.number;

      },
    // 监听pageSize改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getUserList(); // 数据发生改变重新申请数据
    },
    // 监听pageNum改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getUserList(); // 数据发生改变重新申请数据
    },
    // 监听添加用户
    addDialogClosed(){
      this.$refs.ruleForm1.resetFields();// 重置表单项
      this.choice=true
    },
    // 添加用户
    addUser(){
      this.$refs.ruleForm1.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.post("addP",this.form1);
        if (res != "success") {
          return this.$message.error(res);
        }
        this.$message.success("操作成功！！！");
        //隐藏
        this.addDialogVisible = false;
        this.getUserList();
      })
    },
    async deleteUser(id){
        const confirmResult =await this.$confirm('产品信息将从数据库中删除，是否继续？','提示',{
          confirmButtonText:'确定',
          cancelButtonText:'取消',
          type:'warning'
        }).catch(err=>err)
      if(confirmResult!='confirm'){
        return this.$message.info("已取消删除");
      }
      const {data:res} = await this.$http.delete("/deleteProduct?id="+id);
      if(res!='success'){
        return this.$message.error(res)
      }else {
        this.getUserList();
        return this.$message.success("删除成功！")

      }
    }
    ,
    editUserInfo(){
      this.$refs.editFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.post("updateProduct",this.editForm);
        if (res != "success") {
          return this.$message.error(res);
        }
        this.$message.success("操作成功！！！");
        //隐藏
        this.editDialogVisible = false;
        this.getUserList();
      })
    },
    showEditDialog(user){
      this.editDialogVisible=true
      this.editForm.p_account=user.p_account
      this.editForm.kind=user.kind
      this.editForm.p_brief=user.p_brief
      this.editForm.p_specification=user.p_specification
      this.editForm.product_id=user.product_id
      this.editForm.p_price=user.p_price
      this.editForm.p_name=user.p_name
    },
    editDialogClosed(){
      this.$refs.editFormRef.resetFields();// 重置表单项
    },

  }
}
</script>

<style scoped>
.con{
  background-color: #FFFFFF;
  margin: 10px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  padding-left: 10px;
  padding-right: 10px;
  padding-top: 10px;
  padding-bottom: 10px;
  overflow: hidden;
}
</style>